﻿/*
 * The MIT License (MIT)
 * Copyright (c) 2012 @donkeysharp
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data.SQLite;

namespace DonkeyCleaner.Core {
    public class MalwareDatabase {
        /// <summary>
        /// Path for local sqlite malware database
        /// </summary>
        private string DatabaseFilename;

        /// <summary>
        /// Theses two are needed to create just a single instance
        /// for working with malware database
        /// </summary>
        private static MalwareDatabase context;
        public static MalwareDatabase Current {
            get {
                if (context == null)
                    context = new MalwareDatabase();

                return context;
            }
        }
        /// <summary>
        /// Initializes the MalwareDatabase class
        /// </summary>
        private MalwareDatabase() {
            DatabaseFilename = "malwaredb.s3db";
        }
        /// <summary>
        /// Checks if specific hash is in the malware database
        /// </summary>
        /// <param name="hash">Hash to be checked</param>
        /// <returns>True if hash is in the malware database</returns>
        public bool CheckHash(string hash) {
            SQLiteConnection conn = new SQLiteConnection(string.Format("Data Source={0}", DatabaseFilename));
            string sql = "select * from malware where md5=@hash";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            cmd.Parameters.AddWithValue("@hash", hash);

            bool result = false;
            try {
                conn.Open();
                SQLiteDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows) {
                    result = true;
                }
                reader.Close();
            }
            catch (Exception ex) {
                throw ex;
            }
            finally {
                conn.Close();
            }

            return result;
        }
        /// <summary>
        /// Adds a Malware object in local malware database
        /// if this malware doesn't exist
        /// </summary>
        /// <param name="malware">Malware object to add in database</param>
        public void AddMalware(Malware malware) {
            // Checks if given hash doesn't exist in database
            if (CheckHash(malware.MD5Sum))
                return;

            SQLiteConnection conn = new SQLiteConnection(string.Format("Data Source={0}", DatabaseFilename));
            string sql = "insert into malware(md5, name, filesize) values(@hash, @name, @filesize)";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            cmd.Parameters.AddWithValue("@hash", malware.MD5Sum);
            cmd.Parameters.AddWithValue("@name", malware.Name);
            cmd.Parameters.AddWithValue("@filesize", malware.Size);

            try {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                throw ex;
            }
            finally {
                conn.Close();
            }
        }
    }
}
